package xuegao.study.mall.oauth2.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.model.oauth.OauthClientDetails;
import xuegao.study.mall.oauth2.service.AppService;

import java.util.List;

@Api(tags = "AppController")
@Tag(name = "AppController", description = "应用管理相关接口")
@RequestMapping(value = "/auth/app")
@RestController
public class AppController {

    @Autowired
    private AppService appService;

    @ApiOperation("创建一个app")
    @PostMapping(value = "/create")
    public CommonResult<Integer> create(@RequestBody OauthClientDetails details) {
        int count = appService.create(details);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("更新app")
    @PostMapping(value = "/update")
    public CommonResult<Integer> update(@RequestBody OauthClientDetails details) {
        int count = appService.update(details);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取app列表")
    @GetMapping(value = "/list")
    public CommonResult<List<OauthClientDetails>> list() {
        List<OauthClientDetails> list = appService.listApp();
        return CommonResult.success(list);
    }

    @ApiOperation("删除app")
    @PostMapping(value = "/delete/{clientId}")
    public CommonResult<Integer> delete(@PathVariable String clientId) {
        int count = appService.delete(clientId);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}
